<template>
  <div class="upload">
    <div class="upload-uploaded" v-if="select.uploaded">
      <div class="info">
        <div class="tag">上传</div>
        <div class="name">{{ select.uploaded.name }}</div>
      </div>
      <div class="control">
        <div class="duration">{{ select.uploaded.duration }}</div>
        <t-button class="start" size="small" @click="action.listen">试听</t-button>
        <img class="delete" src="@renderer/assets/images/icons/icon-del.png" @click="action.deleteAudio" title="删除" />
      </div>
    </div>
    <div class="upload-box" v-else>
      <t-button class="btn" theme="default" @click="action.uploadAudio" :loading="state.uploading">
        <template #icon>
          <AddIcon />
        </template>
        添加音频</t-button>
      <div class="tip">
        <img class="icon" src="@renderer/assets/images/icons/icon-alert.png" />
        <span>单次最多上传1个录音文件；支持mp3、wav、flac、m4a文件，单个录音时长小于30分钟，请上传纯干音文件，背景音、噪音会影响视频合成效果哦～</span>
      </div>
    </div>

  </div>
</template>
<script setup>
import { reactive } from 'vue'
import { AddIcon } from 'tdesign-icons-vue-next'
import { millisecondsToTime } from '@renderer/utils'
import { MessagePlugin } from 'tdesign-vue-next'
import { Client } from '@renderer/client'

const props = defineProps({
  listener: {
    type: Object,
    default: () => ({})
  }
})


const state = reactive({
  uploading: false,
})

const select = defineModel({})

const action = {
  async uploadAudio() {
    const filePath = await Client.file.selectAudio()
    if (filePath) {
      state.uploading = true
      try {
        const info = await Client.file.getAudioInfo(filePath)
        if (action.check(info)) {
          select.value.uploaded = {
            name: info.name,
            audioUrl: filePath,
            duration: millisecondsToTime(info.duration * 1000)
          }
        }
      } catch (err) {
        console.error(err)
      } finally {
        state.uploading = false
      }
    }
  },
  check(audioInfo) {
    if (!audioInfo.isOK) {
      MessagePlugin.error(audioInfo.msg || '音频上传失败')
      return false
    }
    if (audioInfo.duration > 60 * 30) {
      MessagePlugin.error('音频时长不能超过30分钟')
      return false
    }
    return true
  },
  deleteAudio() {
    const { audioUrl } = props.listener.current()
    if (audioUrl === select.value.uploaded?.audioUrl) {
      props.listener.close()
    }
    select.value.uploaded = null
  },
  listen() {
    if (!select.value.uploaded) return
    const { name, audioUrl, duration } = select.value.uploaded

    props.listener.listen({ name, audioUrl, duration })
  }
}


</script>
<style lang="less" scoped>
.upload {
  &-box {
    display: flex;
    align-items: center;
    gap: 16px;
    --td-bg-color-secondarycontainer: transparent;
    --td-bg-color-specialcomponent: #ffffff;  // 深色背景改为白色
    --td-text-color-primary: #333333;  // 白色文字改为深灰色
    --td-text-color-placeholder: rgba(0, 0, 0, 0.4);  // 半透明白色改为半透明黑色

    .btn {
      flex: none;
      height: 28px;
      background: #0052d9;  // 深色背景改为品牌蓝色
      border-radius: 4px;
      border: none;
      font-weight: 500;
      font-size: 12px;
      padding: 0 10px;
      color: #ffffff;  // 确保按钮文字为白色
      transition: background-color 0.2s ease;  // 添加过渡效果

      &:hover {
        background: rgba(0, 82, 217, 0.9);  // 添加悬停效果
      }
    }

    .tip {
      flex: 1;
      padding: 4px 8px;
      background: rgba(255, 147, 47, 0.12);  // 保留原有的警告色背景
      border-radius: 4px;
      font-weight: 400;
      font-size: 10px;
      color: #FF932F;  // 保留原有的警告色文字
      line-height: 16px;
      display: flex;
      align-items: center;
      border: 1px solid rgba(255, 147, 47, 0.2);  // 添加淡橙色边框增强视觉定义

      .icon {
        width: 12px;
        height: 12px;
        margin-right: 8px;
      }
    }
  }

  &-uploaded {
    color: #333333;  // 白色文字改为深灰色
    padding: 12px;
    width: 100%;
    height: 90px;
    background: #f5f5f5;  // 深色背景改为浅灰色
    border-radius: 4px;
    border: 1px solid #e0e0e0;  // 添加边框增强视觉定义
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    transition: all 0.2s ease;  // 添加过渡效果

    &:hover {
      background: #f0f0f0;  // 添加悬停效果
      border-color: #d0d0d0;
    }

    .info {
      display: flex;
      align-items: center;
      gap: 6px;
      font-weight: 400;
      font-size: 12px;
      color: #333333;  // 白色文字改为深灰色
      line-height: 20px;

      .tag {
        width: 32px;
        height: 16px;
        background: rgba(48, 156, 255, 0.1);  // 保留原有的蓝色背景
        border-radius: 2px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #309CFF;  // 保留原有的蓝色文字
        border: 1px solid rgba(48, 156, 255, 0.2);  // 添加淡蓝色边框增强视觉定义
      }
    }

    .control {
      display: flex;
      align-items: center;
      gap: 12px;

      .duration {
        margin-left: 6px;
        font-weight: 400;
        font-size: 12px;
        color: rgba(0, 0, 0, 0.4);  // 半透明白色改为半透明黑色
        line-height: 14px;
      }

      .start {
        margin-left: auto;
        width: 44px;
        height: 22px;
        border-radius: 3px;
        font-size: 12px;
        background-color: #0052d9;  // 确保按钮使用品牌蓝色
        color: #ffffff;  // 确保按钮文字为白色
        border: none;

        &:hover {
          background-color: rgba(0, 82, 217, 0.9);  // 添加悬停效果
        }
      }

      .delete {
        width: 14px;
        height: 14px;
        cursor: pointer;
        color: #ff4d4f;  // 设置删除图标为红色
        transition: color 0.2s ease;  // 添加过渡效果

        &:hover {
          color: #ff7875;  // 添加悬停效果
        }
      }
    }
  }
}

/* 针对文件上传组件的优化 */
:deep(.t-upload) {
  --td-bg-color-container: #ffffff;  // 设置容器背景为白色
  --td-bg-color-component: #f5f5f5;  // 设置组件背景为浅灰色

  .t-upload__dragger {
    background-color: #f9f9f9;  // 拖拽区域背景为浅灰白色
    border: 1px dashed #e0e0e0;  // 边框为浅灰色虚线

    &:hover {
      border-color: #0052d9;  // 悬停时边框变为品牌蓝色
      background-color: rgba(0, 82, 217, 0.05);  // 悬停时背景添加淡蓝色
    }
  }

  .t-upload__tips {
    color: rgba(0, 0, 0, 0.6);  // 提示文字为半透明黑色
  }

  .t-upload__file {
    background-color: #f5f5f5;  // 文件项背景为浅灰色
    border: 1px solid #e0e0e0;  // 文件项边框为浅灰色

    .t-upload__file-name {
      color: #333333;  // 文件名为深灰色
    }

    .t-upload__icon {
      color: rgba(0, 0, 0, 0.6);  // 图标为半透明黑色
    }
  }
}
</style>
